package cn.dnui_wnx725.dnuifood_16110100725.controller;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.fragment.FavorFragment;
import cn.dnui_wnx725.dnuifood_16110100725.fragment.HomeFragment;
import cn.dnui_wnx725.dnuifood_16110100725.fragment.MyFragment;
import cn.dnui_wnx725.dnuifood_16110100725.fragment.SearchFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener{
    LinearLayout tab_favor;
    LinearLayout tab_home;
    LinearLayout tab_search;
    LinearLayout tab_my;
    ImageView img_favor;
    ImageView img_home;
    ImageView img_search;
    ImageView img_my;
    Fragment fragment_favor;
    Fragment fragment_home;
    Fragment fragment_search;
    Fragment fragment_my;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_main);
        tab_home=findViewById(R.id.tab_home);
        tab_favor=findViewById(R.id.tab_favor);
        tab_search=findViewById(R.id.tab_search);
        tab_my=findViewById(R.id.tab_my);
        img_favor=findViewById(R.id.img_favor);
        img_home=findViewById(R.id.img_home);
        img_my=findViewById(R.id.img_my);
        img_search=findViewById(R.id.img_search);
        setSelect(1);
    }

    @Override
    void initEvent() {
        tab_my.setOnClickListener(this);
        tab_search.setOnClickListener(this);
        tab_favor.setOnClickListener(this);
        tab_home.setOnClickListener(this);
    }

    @Override
    void initData() {

    }

    @Override
    public void onClick(View view) {
        resetImage();
        switch (view.getId()){
            case R.id.tab_home:
                setSelect(1);
                break;
            case R.id.tab_search:
                setSelect(2);
                break;
            case R.id.tab_favor:
                setSelect(0);
                break;
            case R.id.tab_my:
                setSelect(3);
                break;
            default:
                break;
        }
    }
    private  void resetImage(){
        img_home.setImageResource(R.drawable.ic_home);
        img_favor.setImageResource(R.drawable.ic_favor);
        img_search.setImageResource(R.drawable.ic_search);
        img_my.setImageResource(R.drawable.ic_my);
    }

    private void setSelect(int i){
        //使用碎片管理器，碎片的切换通过隐藏和显示碎片实现
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        //隐藏所有碎片
        hideFragment(fragmentTransaction);
        //显示id为i的碎片
        switch (i){
            case 0:
                if(fragment_favor==null){
                    fragment_favor=new FavorFragment();
                    fragmentTransaction.add(R.id.content,fragment_favor);
                }else{
                    fragmentTransaction.show(fragment_favor);
                }
                img_favor.setImageResource(R.drawable.ic_favor_fill);
                break;
            case 1:
                if(fragment_home==null){
                    fragment_home=new HomeFragment();
                    fragmentTransaction.add(R.id.content,fragment_home);
                }else{
                    fragmentTransaction.show(fragment_home);
                }
                img_home.setImageResource(R.drawable.ic_home_fill);
                break;
            case 2:
                if(fragment_search==null){
                    fragment_search=new SearchFragment();
                    fragmentTransaction.add(R.id.content,fragment_search);
                }else{
                    fragmentTransaction.show(fragment_search);
                }
                img_search.setImageResource(R.drawable.ic_search_fill);
                break;
            case 3:
                if(fragment_my==null){
                    fragment_my=new MyFragment();
                    fragmentTransaction.add(R.id.content,fragment_my);
                }else{
                    fragmentTransaction.show(fragment_my);
                }
                img_my.setImageResource(R.drawable.ic_my_fill);
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if(fragment_favor != null) {
            fragmentTransaction.hide(fragment_favor);
        }
        if(fragment_home != null) {
            fragmentTransaction.hide(fragment_home);
        }
        if(fragment_search != null) {
            fragmentTransaction.hide(fragment_search);
        }
        if(fragment_my != null) {
            fragmentTransaction.hide(fragment_my);
        }
    }

}
